package yartiss.service.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractListModel;

@SuppressWarnings("serial")
public abstract class  IChartModel<E>  extends AbstractListModel<E>{

	private final Map<String,E> elementsByName = new HashMap<String, E>(); 
	private final ArrayList<E>  elements = new ArrayList<E>();
	

	public List<E> getAll() {
		return elements;
	}
	
	public void add(E e, String name){
		if(!elementsByName.containsKey(name)){
			elementsByName.put(name, e);
			elements.add(e);
			int index = elements.indexOf(e);
			fireContentsChanged(this, index, index);
		}
	}

	@Override
	public int getSize() {
		return elements.size();
	}

	@Override
	public E getElementAt(int index) {
		return (index >=0 && index<elements.size()) ? elements.get(index) : null;
	}

	public E getElementByName(String name) {
		return elementsByName.containsKey(name) ? elementsByName.get(name) : null;
	}
	
	
	public void clear(){
		elementsByName.clear();
		elements.clear();
		fireContentsChanged(this, 0, 0);
	}
	
	
}
